#!/bin/bash
MIRROR_LOCATION="$WEBROOT/wrf"
###SHELLPACK preamble wrf-bench 3.8.1
###SHELLPACK parseargBegin
###SHELLPACK parseargParam --model	WRF_MODEL
###SHELLPACK parseargParam --processes  WRF_PROCESSES
###SHELLPACK parseargParam --threads    WRF_THREADS
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

install-depends gcc gcc-fortran
install-depends curl libcurl-devel
install-depends tcsh

export HDF5_VERSION="hdf5-1_10_5"
export NETCDFC_VERSION="v4.7.3"
export NETCDFFC_VERSION="v4.5.2"
export PNETCDF_VERSION="1.12.1"

###SHELLPACK mpi_setup_deps WRF_OPENMPI_VERSION
###SHELLPACK mpi_setup_env WRF_OPENMPI_VERSION WRF_MPI_PATH WRF_MPI_LIBPATH WRF_MPI_OPTS
###SHELLPACK mpi_setup_shmem

###SHELLPACK check_external_install_required pnetcdfbuild pnetcdfbuild-$PNETCDF_VERSION $PNETCDF_VERSION
###SHELLPACK check_external_install_required hdf5build hdf5build-$HDF5_VERSION $HDF5_VERSION
###SHELLPACK check_external_install_required netcdfcbuild netcdfcbuild-$NETCDFC_VERSION $NETCDFC_VERSION
###SHELLPACK check_external_install_required netcdffcbuild netcdffcbuild-$NETCDFFC_VERSION $NETCDFFC_VERSION
export LD_LIBRARY_PATH=$SHELLPACK_SOURCES/wrfdeps-installed/$MMTESTS_LIBDIR:$LD_LIBRARY_PATH

# Force installation as parallelisation model may have changed
export INSTALL_FORCE=yes
###SHELLPACK check_install_required wrf-$VERSION

case $WRF_MODEL in
conus12km)
	if [ ! -e $SHELLPACK_SOURCES/wrf-$VERSION-installed/test/em_real_bench ]; then
		cd $SHELLPACK_SOURCES/wrf-$VERSION-installed/test || die "Failed to change to test directory"
		cp -r em_real em_real_bench
		wget -q -O $SHELLPACK_SOURCES/conus12km_data_v3.tar.xz $MIRROR_LOCATION/conus12km_data_v3.tar.xz
		if [ $? -ne 0 ]; then
			BENCH_ROOT="https://www2.mmm.ucar.edu/WG2bench/conus12km_data_v3/"
			FILELIST="em_real_bench/namelist.input em_real_bench/sample_diffwrf_output.txt em_real_bench/wrfbdy_d01 em_real_bench/wrf_reference em_real_bench/wrfrst_d01_2001-10-25_00_00_00"
			for FILE in $FILELIST; do
				wget -O $FILE $BENCH_ROOT/`basename $FILE`
				if [ $? -ne 0 ]; then
					die "Failed to download $BENCH_ROOT/$FILE"
				fi
			done
			echo Creating $SHELLPACK_SOURCES/conus12km_data_v3.tar.xz, suggest uploading to a local mirror
			tar -Jcf $SHELLPACK_SOURCES/conus12km_data_v3.tar.xz $FILELIST
		fi
		tar -xf $SHELLPACK_SOURCES/conus12km_data_v3.tar.xz || die "Failed to extract conus12km_data_v3"
	fi

	# Add base state parameters if necessary
	cd $SHELLPACK_SOURCES/wrf-$VERSION-installed/test/em_real_bench
	grep -q use_baseparam_fr_nml namelist.input
	if [ $? -ne 0 ]; then
		echo Adding base state parameters to namelist.input
		sed -i -e '/^ non_hydrostatic.*/a \ use_baseparam_fr_nml                = .t.,' namelist.input
	fi
	;;
*)
	die "Unrecognised model $WRF_MODEL"
esac
###SHELLPACK init_complete

export OMP_NUM_THREADS=$WRF_THREADS
BENCH_CMD="mpirun $WRF_MPI_OPTS $MMTESTS_MPI_OPTS -np $WRF_PROCESSES ./wrf.exe"
log_runtime "OMP_NUM_THREADS=$WRF_THREADS"
log_runtime "OMP_PLACES=$OMP_PLACES"
log_runtime "OMP_PROC_BIND=$OMP_PROC_BIND"
log_cmd "$BENCH_CMD"
log_linkages ./wrf.exe

monitor_pre_hook $LOGDIR_RESULTS $SPECFEM3D_MODEL
case $WRF_MODEL in
conus12km)
	cd $SHELLPACK_SOURCES/wrf-$VERSION-installed/test/em_real_bench
	save_rc $TIME_CMD -o $LOGDIR_RESULTS/wrf-time.$WRF_MODEL   \
		$BENCH_CMD 2>&1 | tee $LOGDIR_RESULTS/wrf-log.$WRF_MODEL
	recover_rc
	if [ $? -ne 0 ]; then
		die "Failed to execute $WRF_MODEL"
	fi
	tar -czf $LOGDIR_RESULTS/rsl-log.tar.gz rsl* sample_diffwrf_output.txt
	;;
*)
	die "Unrecognised model $WRF_MODEL"
esac
monitor_post_hook $LOGDIR_RESULTS $SPECFEM3D_MODEL
